package com.taotao.vo;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

/**
 * navTabAjaxDone是DWZ框架中预定义的表单提交回调函数．
 * 服务器转回navTabId可以把那个navTab标记为reloadFlag=1, 下次切换到那个navTab时会重新载入内容.
 * callbackType如果是closeCurrent就会关闭当前tab
 * 只有callbackType="forward"时需要forwardUrl值
 * navTabAjaxDone这个回调函数基本可以通用了， 如果还有特殊需要也可以自定义回调函数.
 * 如果表单提交只提示操作是否成功, 就可以不指定回调函数. 框架会默认调用DWZ.ajaxDone()
 * <form action="/user.do?method=save" onsubmit="return validateCallback(this,navTabAjaxDone)">
 **
 form提交后返回json数据结构statusCode=DWZ.statusCode.ok表示操作成功, 做页面跳转等操作.
 statusCode=DWZ.statusCode.error表示操作失败, 提示错误原因.
 * statusCode=DWZ.statusCode.timeout表示session超时， 下次点击时跳转到DWZ.loginUrl
 * {"statusCode":"200", "message":"操作成功", "navTabId":"navNewsLi", "forwardUrl":"",
 "callbackType":"closeCurrent"}
 * {"statusCode":"300", "message":"操作失败"}
 * {"statusCode":"301", "message":"会话超时"}
 **/
@Data
@AllArgsConstructor
@Builder
public class AjaxResult {
    private String statusCode;
    private String message;
    private String navTabId;
    private String rel;
    private String callbackType;
    private String forwardUrl;

    public static AjaxResult ok(String statusCode, String message, String navTabId, String rel, String callbackType, String forwardUrl) {
        return new AjaxResult(statusCode, message, navTabId, rel, callbackType, forwardUrl);
    }

    public static AjaxResult ok(String message, String navTabId, String rel, String callbackType, String forwardUrl) {
        return AjaxResult.builder()
                .statusCode(String.valueOf(ApiCode.SUCCESS.getCode()))
                .message(message)
                .navTabId(navTabId)
                .forwardUrl(forwardUrl)
                .callbackType(callbackType)
                .rel(rel)
                .build();
    }

    public static AjaxResult ok(String message,String navTabId) {
        return ok("操作成功", navTabId, null, "closeCurrent", null);
    }



    public static AjaxResult ok(String navTabId) {
        return ok(null,navTabId);
    }

    public static AjaxResult ok() {
        return ok(null,null);
    }

    public static AjaxResult okNoClose() {
        return ok("操作成功", null, null, null, null);
    }

    public static AjaxResult fail() {
        return AjaxResult.builder()
                .statusCode(String.valueOf(ApiCode.FAILURE))
                .message("操作失败")
                .build();
    }
}
